import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.Duration;

public class demo04 {
    public static void test01() throws IOException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        SimpleDateFormat sim1=new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sim2=new SimpleDateFormat("HH-mm-ss-SS");

        String dirTime=sim1.format(System.currentTimeMillis());
        String fileTime= sim2.format(System.currentTimeMillis());

        String fileName="./src/test/images/"+dirTime+"/"+fileTime+".png";

        File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(srcFile,new File(fileName));

        driver.quit();

    }

    public static void test02() throws InterruptedException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.get("https://www.baidu.com");
        driver.findElement(By.cssSelector("#chat-textarea")).sendKeys("加藤惠");
        driver.findElement(By.cssSelector("#chat-submit-button")).click();
//        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(2));
        WebDriverWait webDriverWait = new WebDriverWait(driver, Duration.ofSeconds(5));
        webDriverWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#searchTag > div > div > a:nth-child(6) > span")));
        driver.findElement(By.cssSelector("#searchTag > div > div > a:nth-child(6) > span")).click();
//        driver.findElement(By.cssSelector("#\\31  > div > div._content-border_1q9is_4.content-border_r0TOp > div > div:nth-child(3) > div > div > div > div > div.cos-swiper-content > div.cos-swiper-list.cos-swiper-transition-none > div:nth-child(2) > div > a > p.avatar-p_6jWfg.avatar-subTitle_6Gcsc.cos-space-xs.cos-line-clamp-1.cos-color-text-minor")).click();
        Thread.sleep(2000);
        driver.quit();
    }

    public static void test03() throws InterruptedException {

        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
        driver.navigate().to("https://www.baidu.com");
        Thread.sleep(2000);
        driver.navigate().back();
        Thread.sleep(2000);
        driver.navigate().forward();
        Thread.sleep(2000);
        driver.navigate().refresh();
        Thread.sleep(2000);
        driver.quit();

    }

    public static void alert() throws InterruptedException {
        EdgeOptions options = Driver.getDriver();
        WebDriver driver=new EdgeDriver(options);
//        driver.navigate().to("file:///D:/bitejava/selenium-html/selenium-html/alert.html#");
//        driver.findElement(By.cssSelector("#tooltip")).click();
//        Thread.sleep(2000);
//        Alert alert = driver.switchTo().alert();
////        alert.dismiss();
//        alert.accept();
//        Thread.sleep(2000);
//        driver.quit();
//        driver.navigate().to("file:///D:/bitejava/selenium-html/selenium-html/confirm.html");
//        driver.findElement(By.cssSelector("body > input[type=button]")).click();
//        Thread.sleep(2000);
//        Alert alert = driver.switchTo().alert();
//        alert.dismiss();
//        Thread.sleep(2000);
//        driver.navigate().refresh();
//        Thread.sleep(2000);
//        driver.findElement(By.cssSelector("body > input[type=button]")).click();
//        Thread.sleep(2000);
//        alert = driver.switchTo().alert();
//        alert.accept();
//        Thread.sleep(2000);
//        driver.quit();

        driver.navigate().to("file:///D:/bitejava/selenium-html/selenium-html/Prompt.html");
        driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(2000);
        Alert alert = driver.switchTo().alert();
        System.out.println(alert.getText());
        alert.dismiss();
        Thread.sleep(2000);
        driver.findElement(By.cssSelector("body > input[type=button]")).click();
        Thread.sleep(2000);
        alert=driver.switchTo().alert();
        alert.sendKeys("加藤惠你好");
        Thread.sleep(2000);
        alert.accept();
        Thread.sleep(2000);
        driver.quit();
    }

    public static void main(String[] args) throws IOException, InterruptedException {
//        test03();
        alert();
    }
}
